﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using IFDB.Models.Contexts;
using IFDB.Models.Tables;
using IFDB.ResourceLibrary;
using IFDB.ViewModels.Foundation;
using IFDB.ViewModels.PartialViewModels.Interfaces;

namespace IFDB.ViewModels.PartialViewModels
{
    public class LeagueTableViewModel : BaseViewModel
    {
        public LeagueTableViewModel(Competition competition, DateTime startDate, DateTime enddDate)
        {
            this.Competition = competition;
            this.StartDate = startDate;
            this.EndDate = enddDate;
        }

        public Competition Competition { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }

        public List<LeagueTableItemViewModel> leagueTable;
        public List<LeagueTableItemViewModel> LeagueTable
        {
            get
            {
                if (leagueTable == null)
                {
                    List<LeagueTableItemViewModel> leagueTableList = new List<LeagueTableItemViewModel>();
                    IEnumerable<Match> matches;

                    using (IFDBContext db = new IFDBContext())
                        matches = db.Matches
                            .Include(m => m.Team1)
                            .Include(m => m.Team2)
                            .Where(m => m.CompetitionId == Competition.CompetitionId 
                                && m.MatchDate >= StartDate 
                                && m.MatchDate <= EndDate 
                                && m.MatchStatus == MatchStatus.FullMatch || m.MatchStatus == MatchStatus.ScoreOnly)
                            .ToList();

                    foreach (Match match in matches)
                    {
                        if (leagueTableList.Count(l => l.Team.TeamId == match.Team1Id) == 0)
                            leagueTableList.Add(new LeagueTableItemViewModel() { Team = match.Team1 });

                        if (leagueTableList.Count(l => l.Team.TeamId == match.Team2Id) == 0)
                            leagueTableList.Add(new LeagueTableItemViewModel() { Team = match.Team2 });

                        LeagueTableItemViewModel team1 = leagueTableList.Single(l => l.Team.TeamId == match.Team1Id);
                        LeagueTableItemViewModel team2 = leagueTableList.Single(l => l.Team.TeamId == match.Team2Id);

                        if (match.Team1FT > match.Team2FT)
                        {
                            team1.WonHome++;
                            team2.LostAway++;
                        }
                        else if (match.Team1FT == match.Team2FT)
                        {
                            team1.DrawnHome++;
                            team2.DrawnHome++;
                        }
                        else if (match.Team1FT < match.Team2FT)
                        {
                            team1.LostHome++;
                            team2.WonAway++;
                        }

                        team1.ScoredHome += match.Team1FT != null ? (short)match.Team1FT : (short)0;
                        team1.ConcededHome += match.Team2FT != null ? (short)match.Team2FT : (short)0;

                        team2.ScoredAway += match.Team2FT != null ? (short)match.Team2FT : (short)0;
                        team2.ConcededAway += match.Team1FT != null ? (short)match.Team1FT : (short)0;
                    }

                    leagueTableList.Sort();

                    leagueTable = leagueTableList;
                }

                return leagueTable;
            }
        }
    }
}
